﻿using LeanApp.Bll;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using System;
using System.Windows.Controls;

namespace LeanApp.View
{
    /// <summary>
    /// CapabilityCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class CapabilityCtrl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CapabilityCtrl(string title, SpcBll model)
        {
            InitializeComponent();
            _title = title;
            _model = model;
            InitialChart();
        }

        /// <summary>
        /// 初始化图表
        /// </summary>
        private void InitialChart()
        {
            title.Text = _title;
            Axis yAxis = new Axis()
            {
                MaxLimit = Math.Max(_model.Max, _model.USL) + Math.Max(_model.Max, _model.USL) * 0.05,
                MinLimit = Math.Min(_model.Min, _model.LSL) - Math.Min(_model.Min, _model.LSL) * 0.05
            };
            Axis xAxis = new Axis()
            {
                MaxLimit = 7,
                MinLimit = 0
            };


            RectangularSection[] mySections = new RectangularSection[]
            {
                new RectangularSection
                {
                    // creates a section from 3 to 4 in the X axis
                    Xi = 1,
                    Xj = 3,
                    Yi=_model.Min,
                    Yj=_model.Max,
                    Fill = new SolidColorPaint(new SKColor(0,0,255))
                },

                new RectangularSection
                {
                    Xi = 2,
                    Xj = 5,
                    Yi = _model.USL,
                    Yj = _model.USL+ 0.5,
                    Fill = new SolidColorPaint(new SKColor(255,0, 0))
                },

                new RectangularSection
                {
                    Xi = 2,
                    Xj = 5,
                    Yi = _model.LSL,
                    Yj = _model.LSL+0.5,
                    Fill = new SolidColorPaint(new SKColor(255,0, 0))
                },

                new RectangularSection
                {
                    Xi = 2,
                    Xj = 5,
                    Yi = _model.Average,
                    Yj = _model.Average+0.5,
                    Fill = new SolidColorPaint(new SKColor(0,255, 0))
                },
            };
            chartCapabilityCtrl.Sections = mySections;
            chartCapabilityCtrl.XAxes = new Axis[] { xAxis };
            chartCapabilityCtrl.YAxes = new Axis[] { yAxis };
        }

        /// <summary>
        /// 数据实体
        /// </summary>
        private SpcBll _model = new SpcBll();
        /// <summary>
        /// 标题
        /// </summary>
        private string _title;
    }
}
